/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package acad.dao;

import acad.bean.Aluno;
import acad.connection.ConnectionFactory;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author André Cauper
 */
public class AlunoDao {
    
    Session session;
    Transaction transaction;
    
    List<Aluno> listaAluno = new ArrayList<Aluno>();
    
    public void salvar(Aluno aluno){
        
        try{
            session = new ConnectionFactory().getSessionFactory();
            transaction = session.beginTransaction();
            session.save(aluno);
            transaction.commit();
            session.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, ""+e.getMessage());
            transaction.rollback();
            session.close();
        }
    }
    
    public void alterar(Aluno aluno){
        
        try {

            session = new ConnectionFactory().getSessionFactory();

            transaction = session.beginTransaction();

            session.update(aluno);

            transaction.commit();

            session.close();

        } catch (Exception e) {

            JOptionPane.showMessageDialog(null, ""+e.getMessage());
            transaction.rollback();
            session.close();

        }
    }
    
    public Aluno consultarPorNome(String nome){
        
        session = new ConnectionFactory().getSessionFactory();
        
        Query query = session.createSQLQuery("Select * From Aluno Where bainome = '"+nome+"'").addEntity(Aluno.class);
        
        System.out.println("Select * From Aluno Where alunome = '"+nome+"'");
        
        listaAluno = query.list();
        
        return listaAluno.get(0);
    }
    
    public List<Aluno> listar(){
        
        session = new ConnectionFactory().getSessionFactory();
        
        listaAluno = session.createCriteria(Aluno.class).list();
        
        return listaAluno;
        
    }
    
    public Aluno consultaPorId(int codigo){
        
        session = new ConnectionFactory().getSessionFactory();
        
        Query query = session.createSQLQuery("Select * From Aluno Where alucod = "+codigo+"").addEntity(Aluno.class);
        
        listaAluno = query.list();
        
        session.close();
        
        return listaAluno.get(0);
    }
    
    public List<Aluno> listarAproximado(String nome){

        session = new ConnectionFactory().getSessionFactory();

        Query query = session.createSQLQuery("Select * From Aluno Where alunome like '%"+nome+"%'").addEntity(Aluno.class);

        listaAluno = query.list();

        session.close();

        return listaAluno;
    }
    
    public void excluir(Aluno aluno){
        
        try {
            
            session = new ConnectionFactory().getSessionFactory();
            transaction = session.beginTransaction();
            
            session.delete(aluno);
            transaction.commit();
            session.close();
            
        } catch (Exception e) {
            
            JOptionPane.showMessageDialog(null, ""+e.getMessage());
            transaction.rollback();
            session.close();
            
        }
    }
}